//
//  ShaiDanVC.m
//  danpin
//
//  Created by chuangjia on 3/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "ShaiDanVC.h"
#import "NSString+Extensions.h"
#import "CategarySelectVC.h"
#import "BrandSelectVC.h"
#import "GoodSelectVC.h"
#import "CellConfig.h"
#import "ZToastManager.h"
#import "WTStatusBar.h"
#import "AppDelegate.h"
#import "ZCameraNavigationController.h"
#import "NSString+Extensions.h"

@interface ShaiDanVC ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UITextFieldDelegate,ZCameraDelegate>
{
    
}
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,strong) UITextField     *txtTitle;
@property(nonatomic,strong) UITextField     *txtType;

@property(nonatomic,strong)NSMutableArray *setcionArray;
@property(nonatomic,strong)NSMutableArray *pictureArray;
@property(nonatomic,strong)NSMutableArray *ImageArray;
@property(nonatomic,strong)NSMutableArray *TextArray;
@property(nonatomic,assign)BOOL isCategoryEnable;

@property(nonatomic,assign)int currentImageId;
@property(nonatomic,strong)NSString*photo_id;
@end

@implementation ShaiDanVC
@synthesize rank_id=_rank_id;
@synthesize type=_type;
@synthesize cateDictionary=_cateDictionary;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view
    
    self.view.backgroundColor =[UIColor whiteColor];
    [UMengShare event:CK_SHAIORDERCREATE acc:[_rank_id integerValue]];
    self.isComment = YES;
    self.rightTitle =@"发布";
    self.navTitle =@"发布晒单";

    if (!_cateDictionary) {
        _cateDictionary =[[NSMutableDictionary alloc] init];
    }
    _isCategoryEnable =YES;
    _currentImageId =0;
    //
    if (!_setcionArray) {
        _setcionArray =[[NSMutableArray alloc] init];
    }
    _setcionArray =[NSMutableArray arrayWithObjects:@"标题: ",@"品牌: ",@"型号: ", nil];
    //

    if (!_pictureArray) {
        _pictureArray =[[NSMutableArray alloc] init];
    }
    if (!_ImageArray) {
        _ImageArray =[[NSMutableArray alloc] init];
    }
    [_ImageArray removeAllObjects];
    
    if (!_TextArray) {
        _TextArray =[[NSMutableArray alloc] init];
    }
    [_TextArray removeAllObjects];
    
    _photo_id =[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"photo_id"]] replaceNullString];
  //
    
    //
    [self initUI];
    [self updateDefault];
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    switch (btn.tag) {
        case 0:
        {
            AppDelegate *app = [[UIApplication sharedApplication]delegate];
            app.nav.enableBackGesture=YES;
            if (self.type==0) {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else
            {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
            break;
        case 1:
        {
            [self okAction];
        }
            break;
        default:
            break;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    DLog(@"%s", __PRETTY_FUNCTION__);
}

-(void)dealloc
{
    [self.txtTitle resignFirstResponder];
    self.txtTitle=nil;
    [self.setcionArray removeAllObjects];
    self.setcionArray=nil;
    [self.pictureArray removeAllObjects];
    self.pictureArray=nil;
    [self.ImageArray removeAllObjects];
    self.ImageArray=nil;
    [self.TextArray removeAllObjects];
    self.TextArray=nil;
    self.tbView.dataSource=nil;
    self.tbView.delegate=nil;
    self.tbView =nil;
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(UITextField *)txtTitle
{
    if (!_txtTitle) {
        self.txtTitle = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtTitle.textColor =[UIColor lightGrayColor];
        [self.txtTitle setBorderStyle:UITextBorderStyleNone];
        self.txtTitle.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtTitle.font = FONT(13);
        self.txtTitle.placeholder =@"选填";
        self.txtTitle.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtTitle.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtTitle.returnKeyType = UIReturnKeyDone;
        self.txtTitle.delegate=self;
        [self.txtTitle setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.txtTitle setValue:FONT(13) forKeyPath:@"_placeholderLabel.font"];
    }
    return _txtTitle;
}
-(UITextField*)txtType
{
    if (!_txtType) {
        self.txtType = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtType.textColor =[UIColor lightGrayColor];
        [self.txtType setBorderStyle:UITextBorderStyleNone];
        self.txtType.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtType.font = FONT(13);
        self.txtType.placeholder =@"选填";
        self.txtType.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtType.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtType.returnKeyType = UIReturnKeyDone;
        self.txtType.delegate=self;
        [self.txtType setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.txtType setValue:FONT(13) forKeyPath:@"_placeholderLabel.font"];
    }
    return _txtType;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
//
-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navHeight, self.view.bounds.size.width, self.view.bounds.size.height-self.navHeight) style:UITableViewStyleGrouped];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor =[UIColor whiteColor];
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.tbView.tableFooterView =[ZShare footerView:@""];
    [self.view addSubview:_tbView];
}
#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.pictureArray.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return self.setcionArray.count;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 44;
    }else
    {
        if (indexPath.section-1>=self.pictureArray.count) {
            return 0;
        }
        NSDictionary *dict =[self.pictureArray objectAtIndex:indexPath.section-1];
        return [ShaiDanCell returnCellHeight:dict];
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
    if (indexPath.section==0)
    {
        UILabel *titleLabel =nil;
        UILabel *contentLabel =nil;
        UIView *lineView =nil;
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*2, 0, 50, cell.frame.size.height)];
            [titleLabel setBackgroundColor:[UIColor clearColor]];
            [titleLabel setTextAlignment:NSTextAlignmentCenter];
            [titleLabel setFont:FONT(14)];
            [titleLabel setTextColor:[UIColor darkGrayColor]];
            [cell.contentView addSubview:titleLabel];
            
            contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(50+PHOTO_FRAME_WIDTH*2, 0,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4-50, cell.frame.size.height)];
            [contentLabel setBackgroundColor:[UIColor clearColor]];
            [contentLabel setTextAlignment:NSTextAlignmentLeft];
            [contentLabel setFont:FONT(13)];
            [contentLabel setTextColor:[UIColor lightGrayColor]];
            [cell.contentView addSubview:contentLabel];
            lineView =[[UIView alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*2, cell.frame.size.height-0.5,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, 0.5)];
            lineView.backgroundColor =UIColorFromRGB(0xcccccc);
            [cell.contentView addSubview:lineView];
        }
        NSString *title =[self.setcionArray objectAtIndex:indexPath.row];
        switch (indexPath.row) {
            case 0:
            {
                titleLabel.textColor = [UIColor darkGrayColor];
                titleLabel.text =title;
                 NSString *tstr =[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"title"]] replaceNullString];
                self.txtTitle.frame =CGRectMake(50+PHOTO_FRAME_WIDTH*2, 1, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4-50, cell.frame.size.height);
                if (tstr.length>0) {
                    self.txtTitle.text = tstr;
                }
                
                [cell addSubview:self.txtTitle];
                
            }
                break;
            case 1:
            {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                titleLabel.text =title;
                NSString *cateString = [[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"brand_name"]] replaceNullString];
                if (cateString.length>0) {
                    contentLabel.text = [NSString stringWithFormat:@"%@",cateString];
                }else
                {
                    contentLabel.text = [NSString stringWithFormat:@"%@",@"可选"];
                }
            }
                break;
            case 2:
            {
                titleLabel.textColor = [UIColor darkGrayColor];
                titleLabel.text =title;
                self.txtType.frame =CGRectMake(50+PHOTO_FRAME_WIDTH*2, 1, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4-50, cell.frame.size.height);
                [cell addSubview:self.txtType];
                
            }
                break;
                
            default:
                break;
        }
        return cell;
    
    }else
    {
       
        ShaiDanCell *cell = (ShaiDanCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[ShaiDanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj ,id key,NSIndexPath *indexPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
       
        if (indexPath.section-1 >=self.pictureArray.count) {
            return cell;
        }
        NSDictionary *dict =[self.pictureArray objectAtIndex:indexPath.section-1];
        if (indexPath.section-1 >self.ImageArray.count) {
            return cell;
        }
        id image = [self.ImageArray objectAtIndex:indexPath.section-1];
        NSString *index=[NSString stringWithFormat:@"%d",(int)(indexPath.section-1)];
        [cell showInfo:dict other:index key:image indexPath:indexPath];
        return cell;
    }
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.txtTitle resignFirstResponder];
    [self.txtType resignFirstResponder];
    AppDelegate *app = [[UIApplication sharedApplication]delegate];
    app.nav.enableBackGesture=YES;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section==0)
    {
        if (indexPath.row==1) {
            BrandSelectVC *cs =[[BrandSelectVC alloc] init];
            __weak typeof (self) wSelf = self;
            [cs setSelectedCallBack:^(id date , id key) {
                
                if (date && [date isKindOfClass:[NSDictionary class]]) {
                    wSelf.cateDictionary = date;
                    [wSelf.tbView reloadData];
                }
            }];
            [self.navigationController pushViewController:cs animated:YES];
            cs=nil;
        }
        
    }
  
}
-(void)cellAction:(BEventType)type views:(UIView *)v obj:(id)obj key:(id)key indexPath:(NSIndexPath *)indexPath
{
    [self.txtTitle resignFirstResponder];
    switch (type) {
        
        case BEventType_shaidan:
        {
            self.currentImageId = [[obj objectForKey:@"id"] intValue];
            NSString *image =[NSString stringWithFormat:@"%@",[obj objectForKey:@"image"]];
            if([image isEqualToString:@"1"])
            {
                UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                              initWithTitle:nil
                                              delegate:self
                                              cancelButtonTitle:@"取消"
                                              destructiveButtonTitle:nil
                                              otherButtonTitles:@"更换图片", @"删除图片",nil];
                actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
                [actionSheet showInView:self.view];
            }else{
               [self showPicker];
            }
    
        }
            break;
        case BEventType_shaidanTxt:
        {
           // 主线程执行：
            self.currentImageId = [[obj objectForKey:@"id"] intValue];
            DLog(@"------------------%@---%@",obj,key);
            NSString *txts =[NSString stringWithFormat:@"%@",key];
            [self updateText:txts];
           
        }
            break;
        case BEventType_shaidanPosition:
        {
            UITableViewCell *cell =[self.tbView cellForRowAtIndexPath:indexPath];
            DLog(@"--------cell-----------%@",NSStringFromCGRect(cell.frame));
            [self.tbView setContentOffset:CGPointMake(0, cell.frame.origin.y+cell.frame.size.height/2) animated:YES];
        }
            break;
        default:
            break;
    }
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 0:  //修改图片
            [self showPicker];
            break;
        case 1:  //删除图片
            [self updateImage:nil];
            break;
    }
}
//
- (void)showPicker
{
    ZCameraNavigationController *viewcontroller =[ZCameraNavigationController newWithCameraDelegate:self];
    [self presentViewController:viewcontroller animated:NO completion:nil];

}
- (void)cameraDidTakePhoto:(UIImage *)image
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self updateImage:image];
   
}
- (void)cameraDidCancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imageFitlerProcessDone:(UIImage *)image
{
    [self updateImage:image];
}

//图片文字编辑
-(void)updateText:(NSString*)txtStr
{
    __weak typeof (self) wSelf = self;
    [self.pictureArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        DLog(@"updateText===>idx=%lu, id=%@", (unsigned long)idx, obj);
        if (obj && [obj isKindOfClass:[NSDictionary class]])
        {
            int index =[[obj objectForKey:@"id"] intValue];
            if (index==wSelf.currentImageId)
            {
                NSMutableDictionary *temp = [obj mutableCopy];
                [temp setValue:[obj objectForKey:@"id"] forKey:@"id"];
                [temp setValue:[obj objectForKey:@"image"] forKey:@"image"];
                [temp setValue:txtStr forKey:@"txt"];
                //
                [wSelf.TextArray replaceObjectAtIndex:idx withObject:txtStr];
                [wSelf.pictureArray replaceObjectAtIndex:idx withObject:temp];
                temp =nil;
            }
        }
    }];
    
    [self updateDefault];
}
//图片编辑
-(void)updateImage:(UIImage*)image
{
    __weak typeof (self) wSelf = self;
    [self.pictureArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//        DLog(@"updateImage===>idx=%lu, id=%@", (unsigned long)idx, obj);
        if (obj && [obj isKindOfClass:[NSDictionary class]])
        {
            int index =[[obj objectForKey:@"id"] intValue];
            if (index==wSelf.currentImageId)
            {
                NSMutableDictionary *temp = [obj mutableCopy];
                [temp setValue:[obj objectForKey:@"id"] forKey:@"id"];
                [temp setValue:[obj objectForKey:@"txt"] forKey:@"txt"];
                if(image)
                {
                    [temp setValue:@"1" forKey:@"image"];
                    [temp setValue:[NSString stringWithFormat:@"%f",image.size.width] forKey:@"width"];
                    [temp setValue:[NSString stringWithFormat:@"%f",image.size.height] forKey:@"height"];
                    [self.ImageArray replaceObjectAtIndex:idx withObject:image];
                }else{
                    [temp setValue:@"0" forKey:@"image"];
                    [self.ImageArray replaceObjectAtIndex:idx withObject:[NSNull null]];
                }
                [wSelf.pictureArray replaceObjectAtIndex:idx withObject:temp];
                temp =nil;
            }
        }
    }];
    
    [self updateDefault];
}
//
-(void)updateDefault
{
     [self updateEmptyDefault];
    id pict =[self.pictureArray lastObject];
    if (pict && [pict isKindOfClass:[NSDictionary class]])
    {
        int index =[[pict objectForKey:@"id"] intValue];
        NSString *txt =[NSString stringWithFormat:@"%@",[pict objectForKey:@"txt"]];
        NSString *image =[NSString stringWithFormat:@"%@",[pict objectForKey:@"image"]];
        if (txt.length==0 && [image isEqualToString:@"0"]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_tbView reloadData];
                });
            return;
        }else{
            NSMutableDictionary *temp1 = [NSMutableDictionary dictionary];
            [temp1 setValue:[NSString stringWithFormat:@"%d",index+1] forKey:@"id"];
            [temp1 setValue:@"0" forKey:@"image"];
            [temp1 setValue:@"" forKey:@"txt"];
            [self.pictureArray  insertObject:temp1 atIndex:self.pictureArray.count];
            [self.ImageArray  insertObject:[NSNull null] atIndex:self.ImageArray.count];
            [self.TextArray  insertObject:[NSNull null] atIndex:self.TextArray.count];
        }
    }else{
        NSMutableDictionary *temp1 = [NSMutableDictionary dictionary];
        [temp1 setValue:[NSString stringWithFormat:@"%d",(int)self.pictureArray.count] forKey:@"id"];
        [temp1 setValue:@"0" forKey:@"image"];
        [temp1 setValue:@"" forKey:@"txt"];
        [self.pictureArray  insertObject:temp1 atIndex:self.pictureArray.count];
        [self.ImageArray  insertObject:[NSNull null] atIndex:self.ImageArray.count];
        [self.TextArray  insertObject:[NSNull null] atIndex:self.TextArray.count];
    }
   
    dispatch_async(dispatch_get_main_queue(), ^{
      [_tbView reloadData];
    });
}
-(void)updateEmptyDefault
{
    __weak typeof (self) wSelf = self;
    [self.pictureArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//        DLog(@"updateEmptyDefault===>idx=%lu, id=%@", (unsigned long)idx, obj);
        if (obj && [obj isKindOfClass:[NSDictionary class]])
        {
            NSString *txt =[NSString stringWithFormat:@"%@",[obj objectForKey:@"txt"]];
            NSString *image =[NSString stringWithFormat:@"%@",[obj objectForKey:@"image"]];
            if (txt.length==0 && [image isEqualToString:@"0"])
            {
                [wSelf.pictureArray removeObjectAtIndex:idx];
                if (idx<wSelf.ImageArray.count) {
                    id imagea =[wSelf.ImageArray objectAtIndex:idx];
                    if (imagea) {
                        [wSelf.ImageArray  removeObjectAtIndex:idx];
                    }
                }
                if (idx<wSelf.TextArray.count) {
                    id txta =[wSelf.TextArray objectAtIndex:idx];
                    if (txta) {
                        [wSelf.TextArray  removeObjectAtIndex:idx];
                    }
                }
            }
        }
    }];
}
//发布
-(void)okAction
{

    [self.tbView reloadData];
    __block BOOL isHaveImage = NO;
    [self.ImageArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (obj!=nil&&![obj isKindOfClass:(NSNull.class)])
        {
            isHaveImage = YES;
        }
    }];
    if (isHaveImage==NO || self.ImageArray.count==0 ||(self.ImageArray.count==1&&[self.ImageArray containsObject:[NSNull null]])) {
        [[ZToastManager ShardInstance] showtoast:@"请设置晒单图片"];
        return;
    }
    [self updateEmptyDefault];
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"cate_id"]] replaceNullString] forKey:@"cate_id"];
    [dict setValue:[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"cate_name"] ]replaceNullString] forKey:@"cate_name"];
    [dict setValue:[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"group_id"] ]replaceNullString] forKey:@"group_id"];
    [dict setValue:[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"group_name"] ]replaceNullString] forKey:@"group_name"];
    [dict setValue:[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"brand_id"] ]replaceNullString] forKey:@"brand_id"];
    [dict setValue:[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"brand_name"] ]replaceNullString] forKey:@"brand_name"];
    NSString *pid =[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"photo_id"] ]replaceNullString];
    if (pid && pid.length>0 && ![pid isEqualToString:@"(null)"]) {
        [dict setValue:@"0" forKey:@"new_ware"];
    }else{
        [dict setValue:@"1" forKey:@"new_ware"];
    }
    [dict setValue:pid forKey:@"photo_id"];
    [dict setValue:[[NSString stringWithFormat:@"%@",self.txtTitle.text]replaceNullString] forKey:@"title"];
    [dict setValue:[[NSString stringWithFormat:@"%@",self.txtType.text]replaceNullString] forKey:@"model_name"];
    
    [dict setValue:[[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"price"]]replaceNullString] forKey:@"price"];
    [dict setValue:[NSString stringWithFormat:@"%d",(int)self.pictureArray.count] forKey:@"file_count"];
   
    if (self.TextArray.count>0) {
        NSData * JSONData = [NSJSONSerialization dataWithJSONObject:self.TextArray
                                                            options:kNilOptions
                                                              error:nil];
        [dict setObject:JSONData forKey:@"image_text"];
    }
 
    [WTStatusBar setStatusText:@"晒单上传中..." animated:YES];
   
    if (self.type==1) {
           [[ZToastManager ShardInstance] showprogress];
    }
     __weak typeof (self) wSelf = self;
    [BaseModel postShaiDan:dict images:self.ImageArray success:^(AFHTTPRequestOperation *operation, id result) {
        if (result==nil) {
            [[ZToastManager ShardInstance] hideprogress];
            [WTStatusBar clearStatusAnimated:YES];
            return;
        }
        if (result && [result isKindOfClass:[NSDictionary class]]) {
            {
                [WTStatusBar setStatusText:[NSString stringWithFormat:@"%@",[result objectForKey:@"msg"]] animated:YES];
                [WTStatusBar clearStatusAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"updateLoveList" object:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"updateShaidanList" object:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"updateCommentList" object:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"shaidanok" object:result];
                id score =[result objectForKey:@"score"];
                if (score && [score isKindOfClass:[NSDictionary class]]) {
                    NSString *scoreMsg =[score objectForKey:@"score_msg"];
                    [WTStatusBar setStatusText:scoreMsg timeout:3 animated:YES];
                }
                
            }
        }
    } failure:^(NSError *error) {
        [[ZToastManager ShardInstance] hideprogress];
        [WTStatusBar setStatusText:@"晒单上传失败..." animated:YES];
        [WTStatusBar clearStatusAnimated:YES];
    } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        if (totalBytesExpectedToWrite>0&&totalBytesWritten>0) {
            CGFloat process = (CGFloat)((totalBytesWritten*100)/totalBytesExpectedToWrite);
            [WTStatusBar setProgress:process/100 animated:YES];
        }
        
    } ];
    if (self.type==0||self.type==2) {
        [self.navigationController popViewControllerAnimated:YES];
    }
   
}

@end
